package com.springboot.test.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.test.common.enums.MessageTypeEnum;
import com.springboot.test.common.utils.ConvertUtils;
import com.springboot.test.common.utils.DateTimeUtil;
import com.springboot.test.common.utils.IdGenerator;
import com.springboot.test.entity.*;
import com.springboot.test.entity.mapper.MessageMapper;
import com.springboot.test.service.param.pageparam.BasePageParam;
import com.springboot.test.service.result.MessageDetailResult;
import com.springboot.test.service.result.MessageResult;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class MessageService extends ServiceImpl<MessageMapper, MessageEntity> {

    @Resource
    private MessageTemplateService messageTemplateService;

    @Resource
    private UnAuditReasonService unAuditReasonService;

    @Resource
    private UserService userService;

//    @Resource
//    private GoodsService goodsService;

    public Boolean sendMessageToEveryOne(String title,String message){
        List<UserEntity> list = userService.list();
        List<MessageEntity> messageEntityList = new ArrayList<>();
        for (UserEntity userEntity : list) {
            MessageEntity messageEntity = new MessageEntity();
            messageEntity.setMessageType(99);
            messageEntity.setMessageTitle(title);
            messageEntity.setMessageCode(IdGenerator.getNewId("M"));
            messageEntity.setMessage(message);
            messageEntity.setUserId(userEntity.getUserId());
            messageEntityList.add(messageEntity);
        }
        return saveBatch(messageEntityList);
    }

    public Boolean sendMessage(String userCode,String goodsCode,Integer type,String goodsName,String phone){
        MessageTemplateEntity templateEntity = messageTemplateService.getOneByCode(type);
        MessageEntity messageEntity = new MessageEntity();
        messageEntity.setMessageCode(IdGenerator.getNewId("M"));
        messageEntity.setMessageType(type);
        messageEntity.setUserId(userCode);
        MessageTypeEnum messageTypeEnum = MessageTypeEnum.getByCode(type);
        messageEntity.setMessageTitle(templateEntity.getMessageDescribe());
//        GoodsEntity goodsEntity = goodsService.queryOneByCode(goodsCode);
        String template = templateEntity.getMessageTemplate();
        switch (messageTypeEnum){
            case AUDIT:
            case TRADE:
            case PUBLISH:
            case RE_PUBLISH:
            case UN_PUBLISH:
                template = template.replace("#Time", DateTimeUtil.Date2String(new Date()))
                        .replace("#goodsName",goodsName)
                ;
                break;
            case UN_AUDIT:
                UnAuditReasonEntity unAuditReasonEntity = unAuditReasonService.getOne(new QueryWrapper<UnAuditReasonEntity>().lambda().eq(UnAuditReasonEntity::getGoodsCode, goodsCode));
                template = template.replace("#Time", DateTimeUtil.Date2String(new Date()))
                        .replace("#goodsName",goodsName).replace("#reason",unAuditReasonEntity.getUnAuditReason())
                ;
                break;
            case PAY:

                template=template.replace("#Time", DateTimeUtil.Date2String(new Date()))
                        .replace("#goodsName",goodsName).replace("#phone",phone)
                ;
                break;
            case PAY_BACK:
            case USER_PAY_BACK:
            case PAY_DONE:
            case TRADE_DONE:
                template=template.replace("#goodsName",goodsName);
                break;
        }
        messageEntity.setMessage(template);
        return save(messageEntity);
    }

    public Integer messageCount(String userCode) {
        return count(new QueryWrapper<MessageEntity>().lambda().eq(MessageEntity::getUserId,userCode).eq(MessageEntity::getStatus,0));
    }

    public Page<MessageResult> messagePage(BasePageParam param, String userCode) {
        Page<MessageEntity> page = page(new Page<>(param.getPageNum(), param.getPageSize()), new QueryWrapper<MessageEntity>().lambda().eq(MessageEntity::getUserId, userCode)
                .orderByDesc(MessageEntity::getCreateTime));
        if(CollectionUtils.isEmpty(page.getRecords())){
            return new Page<>(param.getPageNum(), param.getPageSize());
        }
        List<MessageResult> resultList = new ArrayList<>();
        for (MessageEntity record : page.getRecords()) {
            MessageResult messageResult = ConvertUtils.convert(record,MessageResult.class);
            messageResult.setMessage(record.getMessage().length()>5 ? record.getMessage().substring(0,5)+"..." : record.getMessage());
            messageResult.setCreateTime(DateTimeUtil.Date2String(record.getCreateTime()));
            resultList.add(messageResult);
        }
        Page<MessageResult> resultPage = new Page<>();
        resultPage.setCurrent(param.getPageNum());
        resultPage.setSize(param.getPageSize());
        resultPage.setTotal(page.getTotal());
        resultPage.setRecords(resultList);
        return resultPage;
    }

    public Boolean readAll(String userCode) {
        List<MessageEntity> list = list(new QueryWrapper<MessageEntity>().lambda().eq(MessageEntity::getUserId, userCode).eq(MessageEntity::getStatus, 0));
        for (MessageEntity messageEntity : list) {
            messageEntity.setStatus(1);
        }
        return updateBatchById(list);
    }

    public MessageDetailResult detail(String messageCode) {
        MessageEntity messageEntity = getOne(new QueryWrapper<MessageEntity>().lambda().eq(MessageEntity::getMessageCode,messageCode));
        messageEntity.setStatus(1);
        updateById(messageEntity);
        return ConvertUtils.convert(messageEntity,MessageDetailResult.class);
    }
}
